package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.MaterialRequisition;
import com.woniuxy.factory.entity.Schedule;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.mapper.ScheduleMapper;
import com.woniuxy.factory.service.MateralDetailedService;
import com.woniuxy.factory.service.MaterielAppService;
import com.woniuxy.factory.service.ScheduleService;
import com.woniuxy.factory.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleServiceImp implements ScheduleService {

    @Autowired(required = false)
    ScheduleMapper sm;
    @Autowired
    MaterielAppService ms;
    @Autowired
    MateralDetailedService mds;

    //查询
    @Override
    public List<Schedule> selectAllSchedule(Schedule schedule,int page,int limit) {
        page=(page-1)*limit;
        List<Schedule> ls = sm.selectAllSchedule(schedule,page,limit);
        for(Schedule s : ls){
            if(s.getState()==0){
                s.setStateInfo("未完成");
            }else{
                s.setStateInfo("已完成");
            }
        }
        return ls;
    }

    //新增生产
    @Override
    public void insertSchedule(String planid, String wosId) {
        //在加入生产前判断当前申请是否通过，创建申请对象
        MaterialRequisition m = new MaterialRequisition();
        //将当前计划编号赋值给申请对象
        m.setPlanid(planid);
        //查询申请
        List<MaterialRequisition> lmr = ms.selectApplyInfo(m,1,1);
        //遍历判断当前计划申请是否通过
        for(MaterialRequisition mr : lmr){
            if(mr.getApplicationRecord().getApplystatus()==0){
                throw new BusinessException("该计划单未通过，不能加入生产");
            }
        }
        //判断当前计划单是否领料
        String prpId = sm.selectMaterielByPlanid(planid);
        if(prpId==null){
            throw new BusinessException("还未领料，不能加入生产");
        }
        //在加入生产前判断当前订单是否已经加入
        Schedule schedule = sm.selectScheduleByPlanid(planid);
        if(schedule != null){
            throw new BusinessException("该计划单已投入生产，不可重复操作");
        }
        //当有新的生产计划加入，自动生产流水号
        String serialNumber = UuidUtil.get16UUID();
        //插入数据
        sm.insertSchedule(serialNumber,planid,wosId);
    }

    //查询总数
    @Override
    public int selectScheduleCounts(Schedule schedule) {
        int count = sm.selectScheduleCounts(schedule);
        return count;
    }

    //数据回显
    @Override
    public Schedule scheduleEcho(String serialNumber) {
        Schedule s = sm.scheduleEcho(serialNumber);
        return s;
    }

    //更新
    @Override
    public void upadateSChedcule(Schedule schedule) {
        //更新数据之前判断当前产量与计划单总产量是否相等，是就把状态改为已完成
        int sum = sm.selectNumberSum(schedule.getPlanid());
        //判断，如果当前生产量大于或等于计划的量，就把状态改为已完成
        if(sum<=schedule.getNumber()){
            schedule.setState(1);
        }
        sm.upadateSChedcule(schedule);
    }

    //删除
    @Override
    public void deleteSchedule(String serialNumber) {
        sm.deleteSchedule(serialNumber);
    }

    //更新前先判断当前订单是否已完成，已完成的订单不能更新
    @Override
    public void selectScheudleState(String planid) {
        Schedule schedule = sm.selectScheudleState(planid);
        if(schedule!=null && schedule.getState()==1){
            throw new BusinessException("该订单已完成");
        }
    }
}
